package com.hdjk.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.liangmutian.mypicker.DateUtil;
import com.hdjk.view.R;
import com.hdjk.view.entity.IntegralEntity;

import java.util.List;

import static com.hdjk.view.R.id.tv_date;

/**
 * Created by sxy on 2017/11/3.
 */

public class IntegralAdapter extends BaseAdapter {
    private List<IntegralEntity> mList;
    private Context mContext;

    public IntegralAdapter(Context context) {
        mContext = context;
    }

    public void setDataList(List<IntegralEntity> list) {
        mList = list;
    }

    @Override
    public int getCount() {
        return mList != null ? mList.size() : 0;
    }

    @Override
    public IntegralEntity getItem(int position) {
        return mList != null ? mList.get(position) : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder;
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_integral_list, null);
            holder.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
            holder.tv_count = (TextView) convertView.findViewById(R.id.tv_count);
            holder.tv_date = (TextView) convertView.findViewById(tv_date);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        IntegralEntity entity = mList.get(position);
        holder.tv_type.setText(entity.getTitle());
        String integral="";
        if (entity.getChangeCredit()>0){
            integral="+"+entity.getChangeCredit();
            holder.tv_count.setTextColor(mContext.getResources().getColor(R.color.bg_common_red));
        }else {
            integral=""+entity.getChangeCredit();
            holder.tv_count.setTextColor(mContext.getResources().getColor(R.color.btn_green_pressed));
        }
        holder.tv_count.setText(integral);
        holder.tv_date.setText(DateUtil.formatDate(entity.getCreateTime(),"yyyy-MM-dd"));
        return convertView;
    }

    class Holder {
        TextView tv_type;
        TextView tv_count;
        TextView tv_date;

    }
}
